/***************************************************************
*  NAME:  
*	    sha256 - Secure Hash Standard.
*           FIPS PUB  180-2
*  SYNOPSIS:
*    	    
*  DESCRIPTION:
*     	    
*  HISTORY:
*      	         dietrich - Oct 18, 2011:  Created.
*
***************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "digest.h"

#define SHA256_RESULT_SIZE 8

// block size 512 bits
#define SHA256_BLOCK_SIZE 64
#define SHA256_INPUT_BLOCK_SIZE (64-8)   // 8 bytes required for length padding





/*
 * sha256
 * - 256 bits output, variants for 384 and 512 bits
 * - sigma functions.
 *  
 */

inline unsigned int maj(unsigned int x, unsigned int y, unsigned int z)
{
  return   (x & y) ^ (x & z) ^ (y & z);
}

inline unsigned int ch(unsigned int x, unsigned int y, unsigned int z)
{
  return   (x & y) ^ (~x & z);
}

// rotate right
inline unsigned int rotr(unsigned int x, unsigned int n)
{
  return (x>>n) | (x<<(32-n));
}

inline unsigned inline shr(unsigned int x, unsigned int n)
{
  return  x>>n;
}



// Sigma rot 0 or 1.
inline unsigned int sigma_rot(unsigned int x, unsigned int i)
{
  if ( i==0 ) {
    // Sigmat rot 0
    return rotr(x,2) ^ rotr(x,13) ^ rotr(x,22);
  } else {
    // Sigmat rot 1 
    return rotr(x,6) ^ rotr(x,11) ^ rotr(x,25);
  }
}

// Sigma shr
inline unsigned int sigma_shr(unsigned int x, unsigned int i)
{
  if (i == 0)
    return rotr(x,7) ^ rotr(x,18) ^ shr(x,3);
  else
    return rotr(x,17) ^ rotr(x,19) ^ shr(x,10);
}



/*
 * initial constants for big endian
 */

unsigned int sha256_initial_hash [ ] = {
  0x6a09e667,
  0xbb67ae85,
  0x3c6ef372,
  0xa54ff53a,
  0x510e527f,
  0x9b05688c,
  0x1f83d9ab,
  0x5be0cd19
};

unsigned  int k [] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};



 


/*
 * block operation:
 * 8  32-bit ints
 * 64-int W
 * static block k
 * 64 iterations.
 */

/* TODO MAJ and CH functions */

void sha256_block_operate(const unsigned char * block, unsigned int hash[])
{
  unsigned int W[64];
  int t;
  
  // convert big endian input to 32-bit integers
  for (t=0; t<16; t++) 
    W[t] = (block[4*t]<<24) | (block[4*t+1]<<16) | (block[4*t+2]<<8) | (block[4*t+3]);

  for (t=16; t<64; t++)
    W[t] = sigma_shr(W[t-2],1) + W[t-7] + sigma_shr(W[t-15],0) + W[t-16];

  for (t=0; t<16; t++) {
    printf ("0x%08x ",W[t]);
    if (t%4 == 3) printf("\n");
  }



  unsigned int a, b, c, d, e, f, g, h;

  a = hash[0];
  b = hash[1];
  c = hash[2];
  d = hash[3];
  e = hash[4];
  f = hash[5];
  g = hash[6];
  h = hash[7];

  for (t=0; t<8; t++) {
    printf("hash %d = 0x%8x\n",t,hash[t]);
  }
    

  for (t=0; t<64; t++) {
    unsigned int T1 = h + sigma_rot(e,1) + ch(e,f,g) + k[t] + W[t];
    unsigned int T2 = sigma_rot(a,0) + maj(a,b,c);
    h = g;
    g = f;
    f = e;
    e = d + T1;
    d = c;
    c = b;
    b = a;
    a = T1 + T2;

    printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",a,b,c,d,e,f,g,h);
  }

  hash[0] += a;
  hash[1] += b;
  hash[2] += c;
  hash[3] += d;
  hash[4] += e;
  hash[5] += f;
  hash[6] += g;
  hash[7] += h;

}


void sha256_finalize(unsigned char * padded_block, int length_in_bits)
{
	padded_block[SHA256_BLOCK_SIZE - 4] = (length_in_bits & 0xFF000000) >> 24;
	padded_block[SHA256_BLOCK_SIZE - 3] = (length_in_bits & 0x00FF0000) >> 16;
	padded_block[SHA256_BLOCK_SIZE - 2] = (length_in_bits & 0x0000FF00) >>  8;
	padded_block[SHA256_BLOCK_SIZE - 1] = (length_in_bits & 0x000000FF);  
}




void new_sha256_digest(digest_ctx * context)
{
	context->dtype = digest_SHA256;
	context->hash_len = 8;
	context->input_len = 0;
	context->block_len = 0;
	context->hash = (unsigned int *) malloc(context->hash_len * sizeof(unsigned int));
	memcpy(context->hash, sha256_initial_hash, context->hash_len * sizeof(unsigned int));
	memset(context->block, 0, DIGEST_BLOCK_SIZE);
	context->block_operate = sha256_block_operate;
	context->block_finalize = sha256_finalize;
	context->new_ctx = new_sha256_digest;
}
